﻿namespace zyn_hy_login
{
    using System;
    using System.IO;

    public class Mk_log
    {
        public void logToDataBase(int userid, string username, string mk_num, string msg, string Logtime)
        {
            try
            {
                string text = "insert mk_rizhi(id2,user_name,mk_num,addtime,operation) ";
                object obj2 = text;
                DBCommon.excuteSql(string.Concat(new object[] { obj2, "values(", userid, ",'", username, "','", mk_num, "','", Logtime, "','", msg, "')" }));
            }
            catch (Exception exception)
            {
                this.logToTextFile(Mk_log_config.LogPath, exception.Message, "[数据库操作失败]");
            }
        }

        public void logToTextFile(string path, string msg, string source)
        {
            if (((path != null) && (msg != null)) && (source != null))
            {
                try
                {
                    if (File.Exists(path))
                    {
                        if (new FileInfo(path).Length < Mk_log_config.LogMaxSize)
                        {
                            this.WriteToFile(path, msg, source);
                        }
                    }
                    else
                    {
                        this.WriteToFile(path, msg, source);
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine("写入文件时出现异常: " + exception);
                }
            }
        }

        private void WriteToFile(string path, string msg, string source)
        {
            StreamWriter writer = new StreamWriter(path, true);
            writer.Write(source + msg);
            writer.Close();
        }
    }
}

